package com.zc.study.rubbitmq.eg2;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
import org.apache.commons.lang.SerializationUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author  作者 ucs_fuqing
 * @date 创建时间：2017年8月11日 下午2:39:51
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class QueueConsumer2 extends PointToPoint implements Runnable,Consumer{

    public QueueConsumer2(String pointName) throws IOException {
        super(pointName);
        // TODO Auto-generated constructor stub
    }
//
    public void run(){
        try {
            //channel.basicConsume(pointName,true,this);
            //RabbitMQ支持消息应答模式
            channel.basicConsume(pointName,false,this);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void handleConsumeOk(String consumerTag) {
        // TODO Auto-generated method stub
        System.out.println("Consumer "+consumerTag +" registered");

    }

    @Override
    public void handleCancelOk(String consumerTag) {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleCancel(String consumerTag) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleDelivery(String consumerTag, Envelope env, BasicProperties props, byte[] body) throws IOException {
        // TODO Auto-generated method stub
        Map map = (HashMap)SerializationUtils.deserialize(body);
        System.out.println("Message Number 11 "+ map.get("tagId") + " received.");
        if ((Integer)map.get("tagId")>5){
           int i =10/0;
        }//消息应答模式开启下面注释,避免业务处理异常导致部分消息遗失
        channel.basicAck(env.getDeliveryTag(), false);
    }

    @Override
    public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleRecoverOk(String consumerTag) {
        // TODO Auto-generated method stub

    }

}